package com.getan.boot.common.encrypt;

import com.getan.boot.common.spring.SpringContextHolder;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.springframework.core.env.Environment;

/**
 * 加解密
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
public class JasyptService {

    public String encrypt(String code) {
        Environment env = SpringContextHolder.getBean(Environment.class);
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setPassword(env.getProperty("jasypt.encryptor.password"));
        config.setAlgorithm(env.getProperty("jasypt.encryptor.algorithm", "PBEWithMD5AndDES"));
        encryptor.setConfig(config);
        return encryptor.encrypt(code);
    }

    public String decrypt(String code) {
        Environment env = SpringContextHolder.getBean(Environment.class);
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setPassword(env.getProperty("jasypt.encryptor.password"));
        config.setAlgorithm(env.getProperty("jasypt.encryptor.algorithm", "PBEWithMD5AndDES"));
        encryptor.setConfig(config);
        return encryptor.decrypt(code);
    }

}
